﻿using System.Collections.Generic;
using System.Text;

namespace _5E.Infrastructure.Domain
{
    public abstract class ValueObjectBase
    {
        private IList<BusinessRule> m_borkenRules = new List<BusinessRule>();

        public ValueObjectBase()
        {

        }

        protected abstract void Validate();

        public void ThrowExceptionIfInvalid()
        {
            m_borkenRules.Clear();
            Validate();
            if (0 < m_borkenRules.Count)
            {
                StringBuilder issues = new StringBuilder();
                foreach (BusinessRule borkenRule in m_borkenRules)
                {
                    issues.AppendLine(borkenRule.Rule);
                }
                throw new ValueObjectIsInvalidException(issues.ToString());
            }
        }

        protected void AddBrokenRule(BusinessRule brokenRule)
        {
            m_borkenRules.Add(brokenRule);
        }
    }
}
